package com.baitiaojun.minio.core.support;


import io.minio.*;
import io.minio.messages.DeleteObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

public class MinioSupport {

    @Autowired
    private MinioClient minioClient;

    /**
     * 初始化桶
     *
     * @throws Exception 抛出异常
     */
    public void createBucket(String bucketName) throws Exception {
        if (!bucketExists(bucketName)) {
            /* 制作桶参数 */
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 验证桶名是否存在
     *
     * @param bucketName 桶名
     * @return 返回信息
     * @throws Exception 抛出异常
     */
    public synchronized boolean bucketExists(String bucketName) throws Exception {
        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    }


    /**
     * 文件上传
     *
     * @param bucketName  桶名
     * @param objectName  路径 + 文件名 + 后缀
     * @param inputStream 流对象
     * @param contentType 文件类型
     * @return 对象写入响应
     * @throws Exception 抛出异常
     */
    public ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream, String contentType) throws Exception {
        return minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .contentType(contentType)
                        .stream(inputStream, inputStream.available(), -1).build());
    }

    /**
     * 上传本地文件
     *
     * @param bucketName  桶名
     * @param objectName  路径 + 文件名 + 后缀
     * @param fileName    文件的路径
     * @param contentType 文件类型
     * @return 对象写入响应
     * @throws Exception 抛出异常
     */
    public ObjectWriteResponse uploadFile(String bucketName, String objectName, String fileName, String contentType) throws Exception {
        return minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .filename(fileName)
                        .contentType(contentType).build());
    }

    /**
     * 通过流上传文件
     *
     * @param bucketName  桶名
     * @param objectName  路径 + 文件名 + 后缀
     * @param inputStream 流对象
     * @return 对象写入响应
     * @throws Exception 抛出异常
     */
    public ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) throws Exception {
        return minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(inputStream, inputStream.available(), -1)
                        .build());
    }

    /**
     * 删除单个文件
     *
     * @param bucket   桶名
     * @param fileName 文件名+后缀
     * @throws Exception
     */
    public void removeFile(String bucket, String fileName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs
                .builder()
                .bucket(bucket)
                .object(fileName)
                .build()
        );
    }

    /**
     * 批量删除文件
     *
     * @param bucket    桶名
     * @param fileNames 文件名+后缀
     */
    public void batchRemoveFile(String bucket, List<String> fileNames) {
        minioClient.removeObjects(RemoveObjectsArgs
                .builder()
                .bucket(bucket)
                .objects(fileNames.stream()
                        .map(DeleteObject::new)
                        .collect(Collectors.toList())
                ).build()
        );
    }

    public GetObjectResponse downloadFile(String bucket, String fileName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder()
                .bucket(bucket)
                .object(fileName).build());
    }
}
